package net.ibizsys.psstudio.ms.springboot.api.srv.mqttapi.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiGettokenRequest;
import com.dingtalk.api.request.OapiUserGetuserinfoRequest;
import com.dingtalk.api.response.OapiGettokenResponse;
import com.dingtalk.api.response.OapiUserGetuserinfoResponse;
import com.taobao.api.ApiException;

/**
 * 实体服务API[PSDEVCENTER]控制类
 */
@RestController
@RequestMapping("/dingtalk")
public class DingTalkRestController {

    /**
     * 凭据(需要缓存)默认7200秒有效期
     */
    public static String access_token = null;

    /**
     * 凭证是否还有效
     */
    public static Boolean isInvalidAccessToken = false;

    /**
     * 钉钉登录请求
     * @param code
     * @return
     * @throws ApiException
     */
    @RequestMapping(value = "login", method = RequestMethod.GET)
    public String ddLogin(@RequestParam String code) throws ApiException {
        String userId = this.getUserId(code);
        if (userId != null) {
            return userId;
        }
        return "用户Id获取失败";
    }

    /**
     * 获取用户唯一标识
     * @param authCode
     * @return
     * @throws ApiException
     */
    protected String getUserId(String authCode) throws ApiException {
        String access_token = this.getAccessToken();
        String userId = null;
        if (access_token != null) {
            DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/user/getuserinfo");
            OapiUserGetuserinfoRequest request = new OapiUserGetuserinfoRequest();
            request.setCode(authCode);
            request.setHttpMethod("GET");
            OapiUserGetuserinfoResponse response = client.execute(request, access_token);
            if (response.getErrcode() == 33001 || response.getErrcode() == 40014) {
                DingTalkRestController.isInvalidAccessToken = false;
                return this.getUserId(authCode);
            }
            userId = response.getUserid();
        }
        return userId;
    }

    /**
     * 获取凭据
     * @return
     * @throws ApiException
     */
    protected String getAccessToken() throws ApiException {
        if (DingTalkRestController.access_token == null || !DingTalkRestController.isInvalidAccessToken) {
            DefaultDingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/gettoken");
            OapiGettokenRequest request = new OapiGettokenRequest();
            request.setAppkey("dingbetoapxowm5kdb7j");
            request.setAppsecret("gId2W0THDpDmQmiy2GPY-FjLn02OxXO8MHWKisp6iF02xycmysulZX9BEH3IPejO");
            request.setHttpMethod("GET");
            OapiGettokenResponse response = client.execute(request);
            if (response.isSuccess()) {
                DingTalkRestController.access_token = response.getAccessToken();
                DingTalkRestController.isInvalidAccessToken = true;
            }
        }
        return DingTalkRestController.access_token;
    }

}